<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{math}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.custom.functions.html" title="Chapter 8. Custom Functions">
<link rel="prev" href="language.function.mailto.html" title="{mailto}">
<link rel="next" href="language.function.textformat.html" title="{textformat}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{math}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.mailto.html">Prev</a> </td>
<th width="60%" align="center">Chapter 8. Custom Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.textformat.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{math}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.math"></a>{math}</h2></div></div></div>
<p>
   <code class="varname">{math}</code> allows the template designer to do math equations
   in the template.
   </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
   Any numeric template variables may be used in the
   equations, and the result is printed in place of the tag.
   </p></li>
<li class="listitem"><p>
   The variables used in the equation are passed as parameters,
   which can  be template variables or static values.
   </p></li>
<li class="listitem"><p>+, -, /, *, abs, ceil, cos,  exp, floor, log, log10, max, min,
   pi, pow, rand, round, sin, sqrt, srans and tan are all valid operators.
   Check the PHP documentation  for further information on these
   <a class="ulink" href="http://php.net/eval" target="_top">math</a> functions.
  </p></li>
<li class="listitem"><p>
   If you supply the <em class="parameter"><code>assign</code></em> attribute, the output of the
   <code class="varname">{math}</code> function will be assigned to this template
   variable instead of  being output to the template.
   </p></li>
</ul></div>
<div class="note" title="Technical Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Technical Note</h3>
<p>
   <code class="varname">{math}</code> is an expensive function in performance due to
   its use of the php <a class="ulink" href="http://php.net/eval" target="_top">
   <code class="varname">eval()</code></a> function. Doing the math in PHP is much
   more  efficient, so whenever possible do the math calculations in the script
   and <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>
   the results to the template. Definitely avoid  repetitive
   <code class="varname">{math}</code> function calls, eg within
    <a class="link" href="language.function.section.html" title="{section},{sectionelse}">
    <code class="varname">{section}</code></a> loops.
  </p>
</div>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Attribute Name</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="center">equation</td>
<td align="center">string</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The equation to execute</td>
</tr>
<tr>
<td align="center">format</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>The format of the result (sprintf)</td>
</tr>
<tr>
<td align="center">var</td>
<td align="center">numeric</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Equation variable value</td>
</tr>
<tr>
<td align="center">assign</td>
<td align="center">string</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Template variable the output will be assigned to</td>
</tr>
<tr>
<td align="center">[var ...]</td>
<td align="center">numeric</td>
<td align="center">Yes</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Equation variable value</td>
</tr>
</tbody>
</table></div>
<div class="example">
<a name="id405478"></a><p class="title"><b>Example 8.21. {math}</b></p>
<div class="example-contents">
<p>
  <span class="bold"><strong>Example a:</strong></span>
  </p>
<pre class="programlisting">

   {* $height=4, $width=5 *}

   {math equation="x + y" x=$height y=$width}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

   9

  </pre>
<p>
    <span class="bold"><strong>Example b:</strong></span>
  </p>
<pre class="programlisting">

   {* $row_height = 10, $row_width = 20, #col_div# = 2, assigned in template *}

   {math equation="height * width / division"
   height=$row_height
   width=$row_width
   division=#col_div#}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

   100

  </pre>
<p>
  <span class="bold"><strong>Example c:</strong></span>
  </p>
<pre class="programlisting">

   {* you can use parenthesis *}

   {math equation="(( x + y ) / z )" x=2 y=10 z=2}

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

   6

  </pre>
<p>
    <span class="bold"><strong>Example d:</strong></span>
  </p>
<pre class="programlisting">

   {* you can supply a format parameter in sprintf format *}

   {math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
   
  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

   9.44

  </pre>
</div>
</div>
<br class="example-break">
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.mailto.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.custom.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.textformat.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{mailto} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {textformat}</td>
</tr>
</table>
</div>
</body>
</html>
